package cn.zhibang.sannuo.mycenter.api;

import cn.zhibang.sannuo.account.service.AAccountnumderService;
import cn.zhibang.sannuo.commons.model.Accountnumber;
import cn.zhibang.sannuo.commons.model.CommonResult;
import cn.zhibang.sannuo.commons.utils.SaltUtil;
import cn.zhibang.sannuo.mycenter.service.MyAccountnumberService;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.SimplePrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author admin
 * @version 1.0.0
 * @ClassName MyCenterApi.java
 * @Description TODO
 * @createTime 2021年03月31日 11:58:00
 */
@RestController
public class MyCenterApi {
    @Autowired private CommonResult commonResult;
    @Autowired private MyAccountnumberService accountnumberService;
    @Autowired private AAccountnumderService aAccountnumderService;

    @ApiOperation("修改个人信息")
    @PostMapping("/upAdminInfo")
    public CommonResult upAdminInfo(String aname, String phone1, String phone2, String yanZheng){

        if(!phone1.equals(phone2)){
            commonResult.setCode(206);
            commonResult.setMaggers("电话号码不匹配！");
            return commonResult;
        }
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        String sendOut = (String)session.getAttribute("sendOut");
        if (!yanZheng.equals(sendOut)){
            commonResult.setCode(207);
            commonResult.setMaggers("验证码错误！");
            return commonResult;
        }

        Accountnumber accountnumber = (Accountnumber)subject.getPrincipal();
        accountnumber.setAname(aname);
        accountnumber.setPhone(phone1);
        accountnumberService.upAccountnumberInfo(accountnumber);
        //获得新的用户信息
        Accountnumber newAccountnumber = aAccountnumderService.selAccountnumber(accountnumber.getAccount());
        PrincipalCollection principals = subject.getPrincipals();
        String realName= principals.getRealmNames().iterator().next();
        PrincipalCollection newPrincipalCollection = new SimplePrincipalCollection(newAccountnumber, realName);
        subject.runAs(newPrincipalCollection);
        commonResult.setCode(200);
        commonResult.setMaggers("修改成功");
        return commonResult;
    }

    @ApiOperation("发送验证码")
    @PostMapping("/send_out")
    public CommonResult send_out(String phone){
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        String sendOut = SaltUtil.createSendOut();
        System.out.println(sendOut);
        commonResult.setCode(200);
        commonResult.setMaggers("发送成功");
        session.setAttribute("sendOut", sendOut);
        return commonResult;
    }

    @ApiOperation("修改密码")
    @PostMapping("/upPassword")
    public CommonResult upPassword(String password, String newPassword, String newPassword1){
        if (!newPassword.equals(newPassword1)){
            commonResult.setCode(204);
            commonResult.setMaggers("两次密码不相同！");
        }
        Subject subject = SecurityUtils.getSubject();
        Accountnumber accountnumber = (Accountnumber)subject.getPrincipal();
        String salt = SaltUtil.salt(password, accountnumber.getSalt());
        if (accountnumber.getApssword().equals(salt)){
            String salt1 = SaltUtil.salt(newPassword, accountnumber.getSalt());
            accountnumber.setApssword(salt1);
            accountnumberService.upAccountnumberInfo(accountnumber);

            subject.logout();
            commonResult.setCode(200);
            commonResult.setUrl("/login");
            commonResult.setMaggers("请重新登录");
            return commonResult;
        }
        commonResult.setCode(205);
        commonResult.setMaggers("密码不正确！");
        return commonResult;
    }

}
